#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "setupkwidget.h"
#include <QMessageBox>
#include "shinecommon.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->system_setup_button,SIGNAL(clicked()),this,SLOT(widgetshow()));
    QObject::connect(ui->openbtn,SIGNAL(clicked()),this,SLOT(openoprt()));
    QObject::connect(ui->closebtn,SIGNAL(clicked()),this,SLOT(closeSerial()));
    QObject::connect(ui->sendbtn,SIGNAL(clicked()),this,SLOT(sendData()));


    flag =false ;
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_system_setup_button_clicked()
{

    if (flag==false)
    {
        if (this->openSerial()==false)
            {
              return ;
            }
        flag =true ;
    }
   else
    {

    }
}



void MainWindow::widgetshow()
{
  SetupkWidget *widget=new SetupkWidget()  ;
  widget->show();
  this->hide();
}
bool MainWindow::openSerial()
{

    struct termios setting;

    setData.devName= "ttyO2"  ;
    fd = open(qPrintable("/dev/" + setData.devName), O_RDWR);
    if (fd == -1) {
        QMessageBox::warning(this, "open error", strerror(errno));
        goto openerr;
    }

    tcgetattr(fd, &setting);
    //  设置波特率
    speed_t speed;

        speed = B115200;

    cfsetispeed(&setting, speed);
    cfsetospeed(&setting, speed);
    cfmakeraw(&setting);
    //  设置数据位
       setting .c_cflag |= CS8;
    //  设置parity

        setting.c_cflag &= ~PARENB;
        setting.c_iflag &= ~INPCK;

    //  设置停止位

        setting.c_cflag &= ~CSTOPB;

    tcflush(fd, TCIFLUSH);
    setting.c_cc[VTIME] = 0;
    setting.c_cc[VMIN] = 0;
    tcsetattr(fd, TCSANOW, &setting);

    monitor = new QSocketNotifier(fd, QSocketNotifier::Read, NULL);
    autoTimer = new QTimer;

    connect(monitor, SIGNAL(activated(int)), this, SLOT(readData()));
    connect(autoTimer, SIGNAL(timeout()), this, SLOT(sendData()));

    monitor->setEnabled(true);
    printf("open serial 2 scuess");

  //  charBox->setCheckState(Qt::Checked);
   // autoSend->setCheckState(Qt::Unchecked);
   // sendSpin->setValue(100);
   // sendInfo->clear();
   // clearData();
    ui->textBrowser->clear();
    return true;
seterr:
    ::close(fd);
openerr:
    return false;


}

void MainWindow :: startAutoSend()
{/*
    if (autoSend->isChecked()) {
        sendSpin->setDisabled(true);
        autoTimer->start(sendSpin->value());
    } else {
        autoTimer->stop();
        sendSpin->setDisabled(false);
    }  */
}

void MainWindow :: sendData()
{
    int ret;

    //write(fd,"12345678",8);
    ui->textBrowser->text()="yyyyy";
    ret = write(fd, ui->textBrowser->text().toAscii(), ui->textBrowser->text().size());
    printf("comport ret = %x\n",ret);

}

void MainWindow :: clearData()
{
    ui->textBrowser_6->clear();
    updateData();
}

void MainWindow :: updateData()
{

        int i;
        QString hexStr;
        for (i = 0; i < revData.size(); i++) {
           // hexStr += QString::number(revData.at(i).toAscii(), 16) + " ";
            hexStr += QString("%1").arg(revData.at(i).toAscii()&0xFF,2, 16,QLatin1Char('0'))+" ";
        }
        //revInfo->setText(hexStr);
        ui->textBrowser_6->setText(hexStr) ;
}

void MainWindow :: readData()
{
    char buf[2000];
    int  ret;
    int  i;
    while (revData.size() > 99999) {
        revData.remove(0, 1000);
    }
    ret = read(fd, buf, sizeof(buf));
    revData  += QByteArray::fromRawData(buf, ret);
        QString hexStr;
#ifdef serial debug
        for (i = 0; i < revData.size(); i++) {
            //hexStr += QString::number(revData.at(i).toAscii(), 16) + " ";
              hexStr += QString("%1").arg(revData.at(i).toAscii()&0xFF,2, 16,QLatin1Char('0'))+" ";
        }
#else
        for (i = 0; i < revData.size(); i++) {
             //hexStr += QString::number(revData.at(i).toAscii()&0xff, 16) + " ";
               hexStr += QString("%1").arg(revData.at(i).toAscii()&0x000000FF,2, 16,QLatin1Char('0'))+" ";
                qDebug() << "receiver len = " << revData.size() << "nei rong:" << hexStr;
        }
#endif
    ui->textBrowser_6->setText(hexStr) ;
}

void MainWindow :: closeSerial()
{
    ::close(fd);
    autoTimer->stop();
    delete monitor;
    delete autoTimer;
    emit serialClosed();
     flag =false ;
}

void MainWindow::openoprt()
{

    if (flag==false)
    {
        if (this->openSerial()==false)
            {
              return ;
            }
        flag =true ;
    }
   else
    {

    }
}
